<template>
	<view class="container">
		<view class="header" :style="{ backgroundImage: 'url(' + headerBg + ')' }"></view>
		<view class="top">
			<u-icon @click="handleClickBack" size="40" color="#fff" name="arrow-left"></u-icon>
		</view>
		<view class="form" :style="{ backgroundImage: 'url(' + formBg + ')' }">
			<u-form :errorType="errorType" :model="form" ref="uForm" :border-bottom="false" label-width="200" :label-style="{'font-size':'30upx'}">
				<u-form-item label="所属银行" left-icon="home" :left-icon-style="{'color':'#AB61FF'}" right-icon="arrow-right" prop="name">
					<u-input placeholder="请选择所属银行" v-model="form.name" type="select" :select-open="actionSheetShow" @click="actionSheetShow = true" />
				</u-form-item>
				<u-form-item label="银行卡号" prop="cardNo" left-icon="coupon" :left-icon-style="{'color':'#AB61FF'}">
					<u-input placeholder="请输入银行卡号" v-model="form.cardNo" />
				</u-form-item>
				<u-form-item label="姓名" prop="userName" left-icon="account" :left-icon-style="{'color':'#AB61FF'}">
					<u-input placeholder="请输入持卡人姓名" v-model="form.userName" />
				</u-form-item>
			</u-form>
			<view class="btn" @click="handleClickAdd">
				提交
			</view>
		</view>
		<u-toast ref="uToast" />
		<u-action-sheet :list="actionSheetList" v-model="actionSheetShow" @click="actionSheetCallback"></u-action-sheet>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					name: '',
					cardNo: '',
					code: '',
					userName: ''
				},
				errorType: ['toast'],
				rules: {
					name: [{
						required: true,
						message: '请选择所属银行',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}],
					cardNo: [{
						required: true,
						message: '请输入银行卡号',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}],
					userName: [{
						required: true,
						message: '请输入持卡人姓名',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['change', 'blur'],
					}]
				},
				actionSheetShow: false,
				actionSheetList: this.$mConstDataConfig.bankList,
				headerBg: this.$mAssetsPath.headerBg,
				formBg: this.$mAssetsPath.formBg
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			handleClickAdd() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						this.saveBankAccount();
					} else {
						console.log('验证失败');
					}
				});
			},
			async saveBankAccount() {
				let data = await this.$apis.saveBankAccount(this.form);
				if (data.code === 200) {
					this.$refs.uToast.show({
						title: '添加成功',
						type: 'success',
						back: 'true'
					})
				}
			},
			actionSheetCallback(index) {
				uni.hideKeyboard();
				this.form.name = this.actionSheetList[index].text;
				this.form.code = this.actionSheetList[index].code;
			},
			handleClickBack(){
				uni.navigateBack({
				    delta: 1
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	page {
		background-color: #f4f4f4;
	}

	.container {
		overflow: hidden;
		.top{
			height: 88upx;
			width: 100%;
			position: absolute;
			top: 0;
			line-height: 88upx;
			padding-left: 10upx;
		}
		.form {
			width: 690upx;
			height: 702upx;
			background-size: auto 100%;
			z-index: 1;
			position: relative;
			margin-left: 30upx;
			margin-top: 320upx;
			padding: 50upx 36upx;

			.btn {
				width: 100%;
				height: 80upx;
				background-color: #AB61FF;
				border-radius: 40upx;
				text-align: center;
				line-height: 80upx;
				color: #fff;
				font-size: 32upx;
				margin-top: 150upx;
			}

			.u-form {}
		}

		.header {
			width: 100%;
			height: 420upx;
			background-size: auto 100%;
			position: absolute;
		}
	}
</style>
